<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">

          <el-col :lg="6" :md="12">
            <el-form-item label="学校名称:">
              <el-input
                clearable
                v-model="where.name"
                placeholder="请输入学校名称"/>
            </el-form-item>
          </el-col>

          <el-col :lg="6" :md="12">
            <el-form-item label="状态:">
              <el-select
                clearable
                v-model="where.status"
                placeholder="请选择状态"
                class="ele-fluid">
                <el-option label="全部" value=""/>
                <el-option label="正常" value="1"/>
                <el-option label="停用" value="2"/>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :lg="6" :md="12">
            <el-form-item label="负责人:">
              <el-input
                clearable
                v-model="where.header"
                placeholder="请输入负责人"/>
            </el-form-item>
          </el-col>
          </el-row>
          <el-row :gutter="15">

          <el-col :lg="6" :md="12">
            <el-form-item label="学校所在地:">
              <el-cascader
                clearable
                v-model="where.city"
                class="ele-fluid"
                placeholder="请选择省市区"
                :options="regions.cityData"
                popper-class="ele-pop-wrap-higher"/>
            </el-form-item>
          </el-col>

          <el-col :lg="10" :md="12">
            <el-form-item label="更新时间:">
              <el-col :lg="8" :md="12">
                <el-date-picker
                type="date"
                class="ele-fluid"
                v-model="where.starttime"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
                placeholder="请选择开始日期"/>
              </el-col>
              <el-col :lg="2" :md="12">
                至
              </el-col>
              <el-col :lg="8" :md="12">
                <el-date-picker
                type="date"
                class="ele-fluid"
                v-model="where.endtime"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
                placeholder="请选择结束日期"/>
              </el-col>
            </el-form-item>
          </el-col>

          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload">查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit(null)"
            v-if="permission.includes('sys:school:add')">添加
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
            v-if="permission.includes('sys:school:dall')">删除
          </el-button>
          <el-button
            size="small"
            type="warning"
            icon="el-icon-unlock"
            class="ele-btn-icon"
            @click="statusBatch(1)">启用
          </el-button>
          <el-button
            size="small"
            type="gray"
            icon="el-icon-lock"
            class="ele-btn-icon"
            @click="statusBatch(2)">停用
          </el-button>
          <!-- <el-button
            @click="showImport=true"
            icon="el-icon-upload2"
            class="ele-btn-icon"
            size="small">导入
          </el-button> -->
          <el-button
            size="small"
            type="success"
            icon="el-icon-download"
            class="ele-btn-icon"
            @click="exportExcel">导出
          </el-button>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link
            type="success"
            :underline="false"
            icon="el-icon-set-up"
            @click="tongbu(row)">同步
          </el-link>
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
            v-if="permission.includes('sys:school:edit')">修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除此学校吗？"
            @confirm="remove(row)">
            <el-link
              type="danger"
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
              v-if="permission.includes('sys:school:delete')">删除
            </el-link>
          </el-popconfirm>
        </template>




          <!-- 状态列 -->
          <template slot="status" slot-scope="{row}">
            <el-switch
              v-model="row.status"
              @change="status(row)"
              :active-value="1"
              :inactive-value="2"/>
          </template>



                        </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <school-edit
      :data="current"
      :visible.sync="showEdit"
      @done="reload"/>
      <!-- 导入弹窗 -->
      <school-import
        :visible.sync="showImport"
        @done="reload"/>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import SchoolEdit from './school-edit';
import SchoolImport from './school-import';
import regions from 'ele-admin/packages/regions';
import {download} from "@/utils/download";
export default {
  name: 'ExamSchool',
  components: {SchoolEdit,SchoolImport},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 省市区数据
      regions: regions,
      // 表格数据接口
      url: '/school/index',
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'id',
          label: 'ID',
          width: 60,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        },
              {
          prop: 'code',
          label: '学校编号',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
                    {
          prop: 'name',
          label: '学校名称',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
                    {
          prop: 'address',
          label: '学校所在地',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
                          {
          prop: 'status',
          label: '状态',
          align: 'center',
          width: 100,
          resizable: false,
          slot: 'status',
        },
                          {
          prop: 'header',
          label: '负责人',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
                    {
          prop: 'sort',
          label: '显示顺序',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
                    {
          prop: 'note',
          label: '备注',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
              {
          prop: 'create_time',
          label: '创建时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          prop: 'update_time',
          label: '更新时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 200,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示导入弹窗
      showImport: false
    };
  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.post('/school/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的学校吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/school/delete', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },

    /* 批量设置状态 */
    statusBatch(status) {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要更新选中的学校吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/school/status', {id: this.selection.map(d => d.id),status: status}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },

    /* 更改状态 */
    status(row) {
      const loading = this.$loading({lock: true});
      let params = Object.assign({
        "id": row.id,
        "status": row.status      })
      this.$http.post('/school/status', params).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
        } else {
          row.status = !row.status ? 1 : 2;
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },

    /**
     * 同步第三方
     */
    tongbu(row) {
      this.$confirm('确定要同步吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true,text:'同步中，请稍候...'});
        let params = Object.assign({"code": row.code})
        this.$http.post('/grade/tongbuGradesList',params).then(res => {
          if (res.data.code === 0) {
            loading.close();
            this.$message.success(res.data.msg);
            // this.reload();
          } else {
            loading.close();
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      });
    },

    /* 导出数据Excel */
    exportExcel() {
      let info = JSON.parse(JSON.stringify(this.where));
      download('school/exportExcel', info);
      //window.open(process.env.VUE_APP_API_BASE_URL + "school/exportExcel?" + info)
      // this.$http
      //   .get("/school/exportExcel", info)
      //   .then((res) => {
      //     let data = res.data;
      //     if (data.code == 0) {
      //       // 下载文件
      //       window.location.href = data.data;
      //       this.$message({
      //         type: "success",
      //         message: "导出成功",
      //       });
      //     } else {
      //       this.$message.error("导出失败");
      //     }
      //   })
    },
                          }
}
</script>

<style scoped>
</style>
